import time
from datetime import datetime

import happybase

def convert_date_to_number(time_date):
    # 根据指定的日期时间格式解析字符串为日期时间对象
    #
    datetime_obj = datetime.strptime(time_date, "%Y/%m/%d")
    # 提取年、月、日部分
    year = datetime_obj.year
    month = datetime_obj.month
    day = datetime_obj.day
    # 将年、月、日转换为可比较的数字
    numeric_date = year * 10000 + month * 100 + day
    return numeric_date


# 连接HBase数据库
connection = happybase.Connection('192.168.139.136')

# 选择要操作的表
table_name = 'hot_search'
table = connection.table(table_name)

# 构造查询条件
value_to_query = '模仿迪丽热巴天使蹲易受伤'
start_date = convert_date_to_number('2024/1/12')
end_date = convert_date_to_number('2024/4/12')

# 构建过滤器字符串
filter_str = f"SingleColumnValueFilter('cf', 'date_number', >=, 'binary:{str(start_date)}') AND SingleColumnValueFilter('cf', 'date_number', <=, 'binary:{str(end_date)}')"

# 执行查询
scan = table.scan(filter=filter_str, limit=10)

# 获取查询结果
results = []
for key, data in scan:
    row_key = key.decode()
    row_data = {column.decode(): value.decode() for column, value in data.items()}
    results.append((row_key, row_data))

# 打印查询结果
for row_key, row_data in results:
    print(f"Row Key: {row_key}")
    for column, value in row_data.items():
        print(f"{column}: {value}")
    print()

# 关闭连接
connection.close()
